package com.lbt.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lbt.eduservice.entity.EduCourse;
import com.lbt.eduservice.entity.EduCourseDescription;
import com.lbt.eduservice.entity.front.CourseQueryVo;
import com.lbt.eduservice.entity.front.CourseWebVo;
import com.lbt.eduservice.entity.vo.CourseInfoVo;
import com.lbt.eduservice.entity.vo.CoursePublishVo;
import com.lbt.eduservice.mapper.EduCourseMapper;
import com.lbt.eduservice.service.EduChapterService;
import com.lbt.eduservice.service.EduCourseDescriptionService;
import com.lbt.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lbt.eduservice.service.EduVideoService;
import com.lbt.servicebase.exceptionhandler.GuliException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-06-02
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    @Autowired
    EduCourseDescriptionService descriptionService;
    @Autowired
    EduChapterService chapterService;
    @Autowired
    EduVideoService videoService;


    @Override
    public String  save(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse=new EduCourse();
        EduCourseDescription description=new EduCourseDescription();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        BeanUtils.copyProperties(courseInfoVo,description);
       // eduCourse.setIsDeleted(0);
        int insert = baseMapper.insert(eduCourse);
        if(insert<=0){
            throw new GuliException(20001,"添加课程信息失败！");
        }

        String cid = eduCourse.getId();
        description.setId(cid);
        boolean save = descriptionService.save(description);
        if(!save){
            throw new GuliException(20001,"添加课程描述信息失败！");
        }
        return cid;
    }

    //根据课程id查询课程基本信息
    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        //1 查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        //2 查询描述表
        EduCourseDescription courseDescription = descriptionService.getById(courseId);
//1268042521355776002  1268042521355776002
        courseInfoVo.setDescription(courseDescription.getDescription());
        BeanUtils.copyProperties(eduCourse,courseInfoVo);
        return courseInfoVo;
    }

    //修改课程信息
    @Override
    public void updateCourseInfoVo(CourseInfoVo courseInfoVo) {
        //1 修改课程表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int update = baseMapper.updateById(eduCourse);
        if(update == 0) {
            throw new GuliException(20001,"修改课程信息失败");
        }

        //2 修改描述表
        EduCourseDescription description = new EduCourseDescription();
        description.setId(courseInfoVo.getId());
        description.setDescription(courseInfoVo.getDescription());
        descriptionService.updateById(description);
    }

    //调用自己写的mapper
    @Override
    public CoursePublishVo getPublishCourseInfo(String id) {
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(id);
        return publishCourseInfo;
    }

    @Override
    public void removeCourse(String courseId) {
        //删除小节

        videoService.removeByCourseId(courseId);
        //删除章节
        chapterService.removeByCourseId(courseId);
        //删除课程描述
        descriptionService.removeById(courseId);
        //删除课程
        int result = baseMapper.deleteById(courseId);
        if(result==0){
            throw new GuliException(20001,"删除失败");
        }
    }

    @Override
    public Map<String, Object> selectPageWeb(Page<EduCourse> pageParam, CourseQueryVo courseQuery) {
        QueryWrapper<EduCourse>wrapper=new QueryWrapper<>();
        if(!StringUtils.isBlank(courseQuery.getSubjectParentId())){
            wrapper.eq("subject_parent_id",courseQuery.getSubjectParentId());
        }
        if(!StringUtils.isBlank(courseQuery.getSubjectId())){
            wrapper.eq("subject_id",courseQuery.getSubjectId());
        }

        if(!StringUtils.isBlank(courseQuery.getBuyCountSort())){
            wrapper.orderByAsc("buy_count");
        }
        if(!StringUtils.isBlank(courseQuery.getPriceSort())){
            wrapper.orderByAsc("price");
        }
        if(!StringUtils.isBlank(courseQuery.getGmtCreateSort())){
            wrapper.orderByAsc("gmt_create");
        }
        baseMapper.selectPage(pageParam,wrapper);
        List<EduCourse> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return map;
    }
    //编写mapper sql
    @Override
    public CourseWebVo getBaseCourse(String courseId) {
        return baseMapper.getBaseCourse(courseId);
    }


}
